package cn.itsource._05topic;

import cn.itsource.utils.RabbitMQUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;

/**
 * @Author: CTL
 * @Description: 主题消费者2
 * @DateTime: 2023年06月13日 18:40
 */

public class Consumer2 {
    /**
     * @Description 主题模式，队列2
     */
    public static final String QUEUE_FANOUT_2 = "queue_fanout_2";
    public static void main(String[] args) throws Exception {
        // 1.获取连接
        Channel channel = RabbitMQUtil.getchannel();

        // 2.消息并发处理数量
        //channel.basicQos(1);

        // 3.创建队列
        channel.queueDeclare(QUEUE_FANOUT_2,true,false,false,null);
        // 4.绑定队列列到交换机
        channel.queueBind(QUEUE_FANOUT_2, ProducerFanout.EXCHANGE_DIRECT,"mq");
        channel.basicConsume(QUEUE_FANOUT_2,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" 主题消费者2'" + message + "'");
                System.out.println(envelope.getDeliveryTag());
                //手动签收
                channel.basicAck(envelope.getDeliveryTag(),false);

            }
        });
    }
}
